//
//  KVStatusPictureView.swift
//  KVBestMicroblog
//
//  Created by 李康卫 on 16/7/24.
//  Copyright © 2016年 李康卫. All rights reserved.
//

import UIKit

class KVStatusPictureView: UIView {
    
    let label = UILabel()//测试用
   
    
    let pictureMargin: CGFloat = 4
    
    let maxOfColumn = 3
    ///保存图片URL数组
    var pictureURLS: [NSURL]? {
        didSet {
//            print(pictureURLS)
            let pictureCount = (pictureURLS?.count)! as Int
//            let pictureCount = Int(arc4random_uniform(10))
             label.text = "有\(pictureCount)"
            //设置frame
            let size = setupFrame(pictureCount)
            //更新约束
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
//            print(pictureURLS)
        }
    }
    var imgViews = [UIImageView]()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.whiteColor()
        //添加图片
        addPictures()
    }
    //添加图片
    func addPictures() {
        //创建UIimageView
        //添加到父容器
        for _ in 0..<9  {
            let imgView = UIImageView()
            imgView.backgroundColor = UIColor(colorLiteralRed: Float(arc4random_uniform(255))/225.0, green: Float(arc4random_uniform(255))/225.0, blue: Float(arc4random_uniform(255))/225.0, alpha: 1)
            
            self.addSubview(imgView)
            
            self.imgViews.append(imgView)
        }
     
        self.addSubview(label)

    }
    func setupFrame(pictureCount: Int) -> CGSize {
        
       
        var numberColumn = maxOfColumn
        if  pictureCount == 1 {
            numberColumn = 1
        } else if pictureCount == 2 || pictureCount == 4 {
            numberColumn = 2
        }
        
        //设置宽高
        let pictureWH: CGFloat = (UIScreen.width() - homeCellSpace*2  - 2*pictureMargin)/3
        
//        for var i = 0;i < pictureCount;i++ {
        for (i,pictureView) in self.imgViews.enumerate() {
         
            //设置图片的拉伸
            pictureView.contentMode = UIViewContentMode.ScaleAspectFill
            pictureView.clipsToBounds = true
//            let pictureView = self.imgViews[i]
            //判断否隐藏
            if i >= pictureCount {
                pictureView.hidden = true
                pictureView.image = nil
            } else {
                pictureView.hidden = false
                //行数
                let row = i % numberColumn
                //列数
                let col = i / numberColumn
                
                //MARK: - error
                let pictutreX: CGFloat = CGFloat(row)*(pictureWH + pictureMargin)
                
                let pictureY: CGFloat = CGFloat(col)*(pictureWH + pictureMargin)
                
                pictureView.frame = CGRect(x: pictutreX, y: pictureY, width: pictureWH, height: pictureWH)
                let url = self.pictureURLS![i]
                pictureView.sd_setImageWithURL(url)
            }
            
          
        }
        label.frame = CGRect(x: 0, y: 0, width: 100, height: 100)//测试
        let totalW: CGFloat = CGFloat(numberColumn)*pictureWH + CGFloat(numberColumn - 1)*pictureMargin
        //返回高度
        if pictureCount == 0 {
            return CGSizeZero
        }
        
        // 总行数 = (总数 + 总列数 - 1) / 总列数, 前提保证都是整数
        let numberOfRow = (pictureCount + numberColumn - 1) / numberColumn
        
        // 计算高度 = 总行数 * 图片高度 + (总行数 - 1) * 图片的间距
        let totalH = CGFloat(numberOfRow) * pictureWH + (CGFloat(numberOfRow) - 1) * pictureMargin
        
        let size = CGSizeMake(totalW, totalH)
        
        return size
        
        
//        for (index,picture) in self.imgViews.enumerate() {
//          //这是一个高端的方法但是我不会用
//        }
    }

}
